//
//  AlertSelectView.m
//  NekaPrice
//
//  Created by yu on 2018/6/27.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "AlertSelectView.h"
static NSString *const CellID = @"CellID";
@implementation AlertSelectView
{
    NSArray *_titles;
    NSInteger _row; //记录选中的cell
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 3;
        self.layer.borderColor = [UIColor colorWithHexString:@"#77a1ff"].CGColor;
        self.layer.borderWidth = 1.0f;
        _titles = @[@"我要加盟", @"授权门店", @"质保查询", @"客服电话"];
        _row = 0;
        [self addSubview:self.tableView];
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
        
    }
    return self;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _tableView.separatorColor = [UIColor blackColor];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellID];
    }
    return _tableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    cell.textLabel.text = _titles[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;

    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 3) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_row inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else {
        _row = indexPath.row;
    }
    
    if (_selectedBlock) {
        _selectedBlock(indexPath.row);
    }
}




@end
